package board;

import java.io.*;
import java.net.*;
import java.util.*;
import javax.servlet.http.*;
import model.*;
import org.apache.struts.action.*;
import org.apache.struts.actions.*;

public class PhotoCommentAction extends DispatchAction {
	
	//get PhotoCommunity List per PhotoBoard Number
	public ActionForward getCommmentList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
	
		request.setCharacterEncoding("utf-8");
		int pbno = Integer.parseInt(request.getParameter("pbno"));
		int cono = Integer.parseInt(request.getParameter("cono"));
		
		PhotoCommentDAO pcDAO = new PhotoCommentDAO();
		Vector<PhotoComment> list = pcDAO.getListPerPbno(pbno);
		
		StringBuffer sb = new  StringBuffer();
		sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
		sb.append("<comments>");
		for(int i=0;i<list.size();i++){
			String mcnick = MemberCommunityDAO.getCommunityNick(list.get(i).getMid(), cono);
			
			sb.append("<comment>");
			sb.append("<pcno>"+list.get(i).getPcno()+"</pcno>");
			sb.append("<pbno>"+list.get(i).getPbno()+"</pbno>");
			sb.append("<mid>"+list.get(i).getMid()+"</mid>");
			sb.append("<mcnick>"+mcnick+"</mcnick>");
			sb.append("<pccomment>"+list.get(i).getPccomment()+"</pccomment>");
			sb.append("<pcdate>"+list.get(i).getPcdate().substring(0,19)+"</pcdate>");
			sb.append("<iconno>"+list.get(i).getIconno()+"</iconno>");
			sb.append("</comment>");
		}
		sb.append("</comments>");
		
		response.setContentType("text/xml");
		response.setCharacterEncoding("utf-8");
		
		PrintWriter out = response.getWriter();
		out.print(sb);		
		out.close();
		
		return null;
	}
	
	public ActionForward photoCmtInsert(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		int pbno = Integer.parseInt(request.getParameter("pbno"));
		String mid = request.getParameter("mid");
		int cono = Integer.parseInt(request.getParameter("cono"));
		String pccomment = URLDecoder.decode(request.getParameter("pccomment"),"utf-8");
		
		PhotoCommentDAO pcDAO = new PhotoCommentDAO();
		PhotoComment pc = new PhotoComment();
		pc.setPbno(pbno);
		pc.setMid(mid);
		pc.setPccomment(pccomment);
		pc.setIconno(1);
		
		pc = pcDAO.insert(pc);
		
		StringBuffer sb = new  StringBuffer();
		sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
		sb.append("<comments>");
		
		String mcnick = MemberCommunityDAO.getCommunityNick(mid, cono);
		
			sb.append("<comment>");
			sb.append("<pcno>"+pc.getPcno()+"</pcno>");
			sb.append("<pbno>"+pc.getPbno()+"</pbno>");
			sb.append("<mid>"+pc.getMid()+"</mid>");
			sb.append("<mcnick>"+mcnick+"</mcnick>");
			sb.append("<pccomment>"+pc.getPccomment()+"</pccomment>");
			sb.append("<pcdate>"+pc.getPcdate().substring(0,19)+"</pcdate>");
			sb.append("<iconno>"+pc.getIconno()+"</iconno>");
			sb.append("</comment>");
		
		sb.append("</comments>");
		
		response.setContentType("text/xml");
		response.setCharacterEncoding("utf-8");
		
		PrintWriter out = response.getWriter();
		out.print(sb);
		out.close();
		return null;
	}

	public ActionForward photoCmtDelete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		request.setCharacterEncoding("utf-8");
		int pcno = Integer.parseInt(request.getParameter("pcno"));
		
		PhotoCommentDAO pcDAO = new PhotoCommentDAO();
		
		StringBuffer sb = new  StringBuffer();
		if(pcDAO.delete(pcno)) sb.append("removed");			
		else sb.append("failed");
		response.setContentType("text/xml");
		response.setCharacterEncoding("utf-8");
		
		PrintWriter out = response.getWriter();
		out.print(sb);	
		out.close();	
		
		return null;
	}
	
}
